//
//  YMHomeSubReusableView.swift
//  NewVersionDemo
//
//  Created by zeng on 2018/3/12.
//  Copyright © 2018年 youmy. All rights reserved.
//

import UIKit
import Kingfisher

private let kCellId = "kCellId"

class YMHomeSubReusableView: UICollectionReusableView {
    
    var cycleTimer : Timer?
    
    var icons:[String]? = ["https://gzhlw-files.oss-cn-shanghai.aliyuncs.com/image/oss/banner/460970116860206.png",
                 "https://gzhlw-files.oss-cn-shanghai.aliyuncs.com/image/oss/banner//4947296697399313_2f54dc784d4a467fb41be973d26176ea.jpg",
                 "https://gzhlw-files.oss-cn-shanghai.aliyuncs.com/image/oss/banner//5360038724966435_950da3fa593f4112a81b2959e4e77599.jpg"]
    
    lazy var pagerView: TYCyclePagerView = {
        let pagerView = TYCyclePagerView()
        pagerView.isInfiniteLoop = true
        pagerView.autoScrollInterval = 3.0
        return pagerView
    }()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        addPagerView()
        
        pagerView.isInfiniteLoop = true
        pagerView.autoScrollInterval = true ? 3.0 : 0
        pagerView.updateData()
        
        pagerView.layout.itemSize = CGSize(width: UIScreen.main.bounds.width - 50, height: 105 - 30)
        pagerView.layout.itemSpacing = 15
        self.pagerView.setNeedUpdateLayout();
    }
    
    func addPagerView() {
        pagerView.dataSource = self
        pagerView.delegate = self
        pagerView.register(UINib.init(nibName: "YMHomeAdCycleViewCell", bundle: nil), forCellWithReuseIdentifier: kCellId)
        
        pagerView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 105)
        addSubview(pagerView)
    }
    
    @IBAction func MoreBtnAction(_ sender: UIButton) {
        print("更多")
    }
    
}

extension YMHomeSubReusableView: TYCyclePagerViewDataSource, TYCyclePagerViewDelegate {
    func numberOfItems(in pageView: TYCyclePagerView) -> Int {
        return icons?.count ?? 0
    }
    
    func pagerView(_ pagerView: TYCyclePagerView, cellForItemAt index: Int) -> UICollectionViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: kCellId, for: index) as! YMHomeAdCycleViewCell
        let url = URL(string: icons?[index % (icons?.count ?? 1)] ?? "")
        cell.imageIV.kf.setImage(with: url)
        cell.imageIV.layer.cornerRadius = 5
        cell.imageIV.clipsToBounds = true
        return cell
    }
    
    func layout(for pageView: TYCyclePagerView) -> TYCyclePagerViewLayout {
        let layout = TYCyclePagerViewLayout()
        layout.itemSize = CGSize(width: pagerView.frame.width, height: pagerView.frame.height)
        layout.itemSpacing = 0
        layout.itemHorizontalCenter = true
        return layout
    }
    
    func pagerView(_ pageView: TYCyclePagerView, didSelectedItemCell cell: UICollectionViewCell, at index: Int) {
        print("点击:\(index)")
    }
}

extension YMHomeSubReusableView {
    class func nibView() -> YMHomeSubReusableView{
        return Bundle.main.loadNibNamed("YMHomeSubReusableView", owner: self, options: nil)?.last as! YMHomeSubReusableView
    }
}
